import {
	defineStore
} from 'pinia';
import api from '@/services/api/memo'

export const useMemoStore = defineStore('memo', {
	state: () => ({
		id: '',
		// assignments, startTimes, endTimes, works, importance
		memoList: []
		// memoList: [["明早10点去汕头大学参观", "2025-10-14 20:49:21", "2025-10-15 10:00:00", "0", 5], ["明早11点去汕头万象城吃午饭", "2025-10-14 20:52:43", "2025-10-15 11:00:00", "0", 2]]
	}),

	actions: {
		async getUserId() {
			try {
				const res = await api.getUserId()
				if (res.code) {
					this.id = res.code
					return this.id
				} else {
					throw new Error('未获取到用户id')
				}
			} catch (error) {
				uni.showToast({
					title: '用户id获取失败',
					icon: 'none'
				})
				throw error
			}
		},

		// 只获取不返回， 需要memoList数据再从data中提取
		async getMemoList() {
			this.id = await this.getUserId()

			try {
				const memoRes = await api.getMemoList(this.id)
				console.log(memoRes)
				this.memoList = memoRes.data.memoList
			} catch (error) {
				console.error("请求错误:", error)
				uni.showToast({
					title: '数据加载失败',
					icon: 'none'
				})
			}
		},
				
		// 获取按照 重要性 排序的memoList
		async getMemoListImpor() {
			this.id = await this.getUserId()
			
			try {
				const memoRes = await api.getMemoListImpor(this.id)
				console.log('impor:', memoRes)
				this.memoList = memoRes.data.memoList
			} catch (error) {
				console.log("请求失败:", error)
				uni.showToast({
					title: '数据加载失败',
					icon: 'none'
				})
			}
		},

		// 提交用户输入
		async postInput(writeText) {
			if (!writeText.trim()) {
				uni.showToast({
					title: '你还啥都没写呢',
					icon: 'none'
				})
				return
			}

			this.id = await this.getUserId()

			try {
				const res = await api.postMemoItem(this.id, writeText)
				console.log(res)
				if (res.statusCode === 200) {
					uni.showToast({
						title: '添加成功',
						icon: 'success'
					})
				} else {
					throw new Error('提交失败') // 该语句会结束当前函数并跳到最近的catch
				}
			} catch (error) {
				uni.showToast({
					title: '提交失败',
					icon: 'none'
				})
				console.log(error)
			}
		},
		
		// 删除备忘录事项
		async deleteMemoItem(name) {
			this.id = await this.getUserId()
			try {
				const res = await api.deleteMemoItem(this.id, name)
				console.log(res)
				if (res.statusCode === 200) {
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					})
				} else {
					throw new Error('删除失败')
				}
			} catch (error) {
				uni.showToast({
					title: '删除失败',
					icon: 'none'
				})
				console.log(error)
			}
		},
		
		// 修改备忘录事项状态
		async changeMemoItem(name) {
			this.id = await this.getUserId()
			console.log(this.id)
			try {
				const res = await api.changeMemoItem(this.id, name)
				console.log(res)
				if (res.statusCode === 200) {
					uni.showToast({
						title: '修改完成状态成功',
						icon: 'success'
					})
				} else {
					throw new Error('修改完成状态失败')
				}
			} catch (error) {
				uni.showToast({
					title: '修改完成状态失败',
					icon: 'none'
				})
				console.log(error)
			}
		},
		
		// 修改备忘录事项重要性
		async setMemoImpor(name, number) {
			this.id = await this.getUserId()
			try {
				const res = await api.postMemoItemImpor(this.id, name, number)
				if(res.statusCode === 200) {
					uni.showToast({
						title: '修改重要性成功',
						icon: 'success'
					})
				} else {
					throw new Error('修改重要性失败')
				}
			} catch (error) {
				uni.showToast({
					title: '修改重要性失败',
					icon: 'none'
				})
				console.log(error)
			}
		}
	}
})